package com.freejava.jdbctest.controller;

import com.freejava.jdbctest.entities.Book;
import com.freejava.jdbctest.services.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BookController {
    @Autowired
    BookService bookService;

    @RequestMapping("/testJdbc")
    public void test() {
        // 测试插入
        Book book1 = new Book();
        book1.setTitle("Python爬虫");
        book1.setPrice(50.50);
        long unixTime = System.currentTimeMillis() / 1000L;
        int nowUnixTime = (int) unixTime;
        book1.setCreated(nowUnixTime);
        int addRes = bookService.add(book1);
        System.out.println("add result is:" + addRes);

        // 查询所有
        List<Book> books = bookService.getAll();
        System.out.println(books);

    }


    @RequestMapping("/getAllBook")
    public List<Book> getAllBook() {
        List<Book> books = bookService.getAll();
        return books;
    }


    @RequestMapping("/getBookById")
    public Book getBookById(int id) {
        Book book = bookService.getBookById(id);
        return book;
    }

    @RequestMapping("/updateBook")
    public int updateBook(Book book) {
        int result = bookService.update(book);
        return result;
    }

}
